// 封装所有和token处理相关的api方法
// getToken
// setToken
// removeToken
// hasToken

import { Channel, History } from '@/types/data'
import { Token } from '@/types/store'

const TOKEN_KEY = 'geek-91-token'
const CHANNEL_KEY = 'geek-91-tokens'
const HISTORY_KEY = 'geek-91-history'

// 获取 token
export const getToken = (): Token => {
  return JSON.parse(localStorage.getItem(TOKEN_KEY) || '{}')
}

// 设置 token
export const setToken = (obj: Token) => {
  localStorage.setItem(TOKEN_KEY, JSON.stringify(obj))
}

// 移除 token
export const removeToken = (): void => {
  localStorage.removeItem(TOKEN_KEY)
}

// 判断有无 token
export const hasToken = (): Boolean => {
  return !!getToken().token
}

// 从本地获取 channels数据
export const getLocalChannels = (): Channel[] => {
  return JSON.parse(localStorage.getItem(CHANNEL_KEY) || '[]')
}

//将channels数据往本地存
export const setLocalChannels = (channels: Channel[]) => {
  localStorage.setItem(CHANNEL_KEY, JSON.stringify(channels))
}

// 获取本地历史
export const getLocalHistories = (): History => {
  return JSON.parse(localStorage.getItem(HISTORY_KEY) || '[]')
}

// 设置本地历史
export const setLocalHistories = (arr: History) => {
  localStorage.setItem(HISTORY_KEY, JSON.stringify(arr))
}

// 移除本地历史
export const removeLocalHistories = () => {
  localStorage.removeItem(HISTORY_KEY)
}
